package com.bigcat.gulimall.gulimallmember.controller;

import com.bigcat.gulimall.common.utils.PageUtils;
import com.bigcat.gulimall.common.utils.R;
import com.bigcat.gulimall.gulimallmember.entity.MemberEntity;
import com.bigcat.gulimall.gulimallmember.feign.CouponFeignService;
import com.bigcat.gulimall.gulimallmember.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author sufer
 * @email 123@qq.com
 * @date 2020-09-08 11:10:52
 */

/**
 * RefreshScope；动态刷新配置中心配置
 */
@RefreshScope
@RestController
@RequestMapping("gulimallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @Value("${member1.user.name}")//从application.properties中获取//不要写user.name，他是环境里的变量
    private String name;

    @Value("${member1.user.age}")
    private Integer age;

    @RequestMapping("/test")
    public R test(){

        return R.ok().put("name",name).put("age",age);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallmember:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimallmember:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallmember:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallmember:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallmember:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @RequestMapping("/coupons")
    public R coupons(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("zhangsan");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("coupons",memberEntity).put("membercoupons",membercoupons);
    }

}
